module.exports = app => {
	const Sequelize = app.Sequelize;
	const user = app.model.define('users', {
		id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
		user_id_github: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: 'github用户ID' },
		department_id: { type: Sequelize.INTEGER, allowNull: true, defaultValue: null, comment: '部门id' },
		username: {type: Sequelize.STRING(60), allowNull: false, comment: '用户名'},
		nickname: {type: Sequelize.STRING(60), allowNull: false, defaultValue: '', comment: '昵称'},
		password: {type: Sequelize.STRING(64), allowNull: false, comment: '用户密码'},
		email: {type: Sequelize.STRING(60), allowNull: false, defaultValue: '', comment: '邮箱'},
		state: {type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '状态：0.停用、1.正常'},
		phone: {type: Sequelize.STRING(15), allowNull: false, defaultValue: '', comment: '手机号'},
		avatar: {type: Sequelize.STRING(255), allowNull: false, defaultValue: '', comment: '头像url'},
		company: {type: Sequelize.STRING(80), allowNull: false, defaultValue: '', comment: '国家'},
		city: {type: Sequelize.STRING(80), allowNull: false, defaultValue: '', comment: '城市'},
		last_login: {type: Sequelize.DATE, allowNull: true, defaultValue: null, comment: '最后登录时间'},
	}, {
		paranoid: true, // 启用软删除
		defaultScope: {
			attributes: { exclude: ['password'] },
		},
		scopes: {
			withPassword: {
				attributes: {},
			},
		},
	})
	user.associate = function() {
		// 用户角色关联
		user.belongsToMany(app.model.Roles, {
			through: app.model.UserRoles,
			foreignKey: 'user_id',
			otherKey: 'role_id'
		});

		// 用户管理的项目（一对多关系）
		user.hasMany(app.model.Projects, {
			foreignKey: 'manager_id',
			as: 'managedProjects'
		});

		// 用户参与的项目（多对多关系）
		user.belongsToMany(app.model.Projects, {
			through: app.model.UserProjects,
			foreignKey: 'user_id',
			otherKey: 'project_id',
			as: 'projects'
		});
	}
	return user;
}